package com.utils.export;

import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestParam;

import com.google.common.collect.Lists;
import com.utils.dateUtil.DateUtil;
import com.wf.course.model.goods.GoodsOrder;
import com.wf.course.model.goods.GoodsOrderRelation;
 


public class GoodsOrderExcelExporter{
	
	/**
	 * 类名称：GoodsOrderExcelExporter
	 * 类描述：实现商品订单信息表导出功能
	 * @version 1.0.0
	 */
	public void exportExcel(HttpServletResponse response,List<GoodsOrder> goodsOrderList) {
		
		// 定义导出的Excel文件名称
		String fileName = "商品订单信息表"+DateUtil.getDateString(new SimpleDateFormat("yyyyMMddHHmmss"));
		//数据处理
		
		//表头list
		List<String> headerList = Lists.newArrayList();
			headerList.add("序号");
			headerList.add("订单编号");
			headerList.add("支付单号");
			headerList.add("订单状态");
			headerList.add("订单支付时间");
			headerList.add("渠道");
			headerList.add("手机号");
			/*headerList.add("订单生成时间");*/
			headerList.add("购买课程详情");
			headerList.add("全场活动");
			headerList.add("使用优惠券");
			headerList.add("购买数量");
			headerList.add("原铜钱");
			headerList.add("新铜钱");
			headerList.add("金币");
			headerList.add("订单总金额");
			headerList.add("实际支付");
			headerList.add("授课老师");
			headerList.add("误差值");
			headerList.add("是否退货");
			headerList.add("配置说明");
			headerList.add("订单备注");
			headerList.add("订单标识");
			/*headerList.add("课程金额");
			headerList.add("运费");
			headerList.add("铜钱使用");*/
			/*headerList.add("收件人");
			headerList.add("收件电话");
			headerList.add("收件人地址");*/
			
		/*	headerList.add("优惠券使用说明");*/
			/*headerList.add("商品购买详情");
			headerList.add("订单备注");*/
		
		//数据集合
		List<List<String>> dataList = Lists.newArrayList();
		//序号
		int colNum = 0;
		//遍历数据
		for(GoodsOrder ord : goodsOrderList){
			colNum++;
			Object value;
			
			//每一行的数据list
			List<String> dataRowList = Lists.newArrayList();
			//序号
			dataRowList.add(String.valueOf(colNum));
			//订单编号
			value = ord.getOrderCode();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//微信支付单编号
			value = ord.getTransaction_id();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//订单状态
			value = ord.getOrderStatus();
			value = value == null ? "" : value;
			//value = value == null ? "" : value;
			if("1".equals(String.valueOf(value))){
				dataRowList.add("待付款");
			}else if("2".equals(String.valueOf(value))){
				dataRowList.add("待收货");
			}else if("3".equals(String.valueOf(value))){
				dataRowList.add("待评价");
			}else if("4".equals(String.valueOf(value))){
				dataRowList.add("已完成");
			}else if("5".equals(String.valueOf(value))){
				dataRowList.add("取消订单");
			}else if ("6".equals(String.valueOf(value))) {
				dataRowList.add("订单已退货");
			}
			
			//订单支付时间
			value = ord.getOrderPayTime();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//来源渠道
			value = ord.getUserSource();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//用户手机
			value = ord.getTelephone();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//课程购买详情
			value = ord.getCourseDetailStr();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//优惠活动
			value = ord.getActivityInfo();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//使用优惠券
			value = ord.getTitle();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//购买数量
			dataRowList.add("");
			
			//原铜钱
			value = ord.getOldCopperCash();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			//新铜钱
			value = ord.getNewCopperCash();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//金币
			value = ord.getGold();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//订单总额
			value = ord.getGoodsAmount();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//实际支付
			value = ord.getOrderAmount();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
            dataRowList.add("");
			
			dataRowList.add("");

			
			dataRowList.add("");
			
			//配置说明
			value = ord.getRemark();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//订单备注
			value = ord.getOrderRemark();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//订单标识
			value = ord.getIdentification();
			value = value ==null ? "":value;
			dataRowList.add(String.valueOf(value));
			
			/*//实体商品金额
			value = ord.getGoodsInfoAmount();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			*/
			
			/*//订单生成时间
			value = ord.getOrderTime();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			/*//课程金额
			value = ord.getCourseInfoAmount();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//邮费
			value = ord.getFreight();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			
			/*//铜钱使用
			value = ord.getCopperCash();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			/*//收件人
			value = ord.getRecipient();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			//收件人电话
			value = ord.getRecipientPhone();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			//收件人地址
			value = ord.getDetailAddress();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			/*//优惠券使用说明
			value = ord.getCoupons_memo();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			
			
		/*	
			//订单商品购买详情
			value = ord.getGoodsDetailStr();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//订单备注
			value = ord.getOrderRemark();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			
			dataList.add(dataRowList);
			
			//明细
			if(ord.getOrderDetailList().size()>0)
			{
				for(GoodsOrderRelation item : ord.getOrderDetailList()){
					dataList.add(outputOrderItem(item));
				}
			}
			
		}
		
		//导出数据
		ExportExcel.doExportExcel(response, fileName, headerList, dataList);
	}
	
	private List<String> outputOrderItem(GoodsOrderRelation ord)
	{
		
		int colNum = 0;
		
		//每一行的数据list
		List<String> dataRowList = Lists.newArrayList();
		

			colNum++;
			Object value;
			
			
			//value = "";
			
			/*for(int i=0; i< colNum; i++)
			{
				dataRowList.add("");
			}*/
			
			//序号
			dataRowList.add("");
			//订单编号
			value = ord.getOrderCode();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//微信支付单编号
			dataRowList.add("");
			
			dataRowList.add("");
			
			dataRowList.add("");
			
			dataRowList.add("");
			
			dataRowList.add("");
			
			//购买课程详情
			value = ord.getGoodsName();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			
			//优惠活动
			value = ord.getActivityCash();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//使用优惠券
			value = ord.getCouponsCash();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//数量
			value = ord.getGoodsCount();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			if(ord.getVersion()==null ||"".equals(ord.getVersion())){
				
				//原铜钱
				value = ord.getCopperCash();
				value = value == null ? "" : value;
				dataRowList.add(String.valueOf(value));	
			}else if(ord.getVersion().equals("2.0")){
				//原铜钱
				value = ord.getCopperCash();
				value = value == null ? "" : value;
				dataRowList.add(String.valueOf(value));	
			}else if(ord.getVersion().equals("3.0")){
				
				//原铜钱
				value = ord.getOldCopperCash();
				value = value == null ? "" : value;
				dataRowList.add(String.valueOf(value));	
				
			}			
			//新铜钱
			value = ord.getNewCopperCash();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//金币
			value = ord.getGold();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//商品销售价格
			value = ord.getGoodsPrice();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//商品实际支付价格
			value = ord.getRealPay();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//授课老师
			value = ord.getSpeakerName();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			if(ord.getVersion()==null ||"".equals(ord.getVersion())){
				//误差值
				value = ord.getErrorvalues();
				value = value == null ? "" : value;
				dataRowList.add(String.valueOf(value));
			}else if(ord.getVersion().equals("2.0")){
				
				//误差值
				value = ord.getErrorvalues();
				value = value == null ? "" : value;
				dataRowList.add(String.valueOf(value));
			}else if(ord.getVersion().equals("3.0")){
				
				//误差值
				value = ord.getErrorvalue();
				value = value == null ? "" : value;
				dataRowList.add(String.valueOf(value));	
			}
			//是否以退单
			value = ord.getBackFlag();
			value = value == null ? "" : value;
			//value = value == null ? "" : value;
			if("1".equals(String.valueOf(value))){
				dataRowList.add("已退货");
			}else {
				dataRowList.add("未退货");
			}
	          dataRowList.add("");
			
			dataRowList.add("");
			/*//来源渠道
			value = ord.getUserSource();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//用户手机
			value = ord.getTelephone();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//课程购买详情
			value = ord.getCourseDetailStr();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//优惠活动
			value = ord.getActivityInfo();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//使用优惠券
			value = ord.getTitle();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			*/
			
			/*
			//订单总额
			value = ord.getGoodsAmount();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//实际支付
			value = ord.getOrderAmount();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			/*//实体商品金额
			value = ord.getGoodsInfoAmount();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			*/
			
			/*//订单生成时间
			value = ord.getOrderTime();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			/*//课程金额
			value = ord.getCourseInfoAmount();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//邮费
			value = ord.getFreight();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			
			/*//铜钱使用
			value = ord.getCopperCash();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			/*//收件人
			value = ord.getRecipient();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			//收件人电话
			value = ord.getRecipientPhone();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			//收件人地址
			value = ord.getDetailAddress();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			/*//优惠券使用说明
			value = ord.getCoupons_memo();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			
			
		/*	
			//订单商品购买详情
			value = ord.getGoodsDetailStr();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));
			
			//订单备注
			value = ord.getOrderRemark();
			value = value == null ? "" : value;
			dataRowList.add(String.valueOf(value));*/
			
			
			return dataRowList;
	}
}
